package com.kh.pajx.sop.service.order.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.order.IReplaceCardOrderDao;
import com.kh.pajx.sop.dao.order.IReplaceCardOrderRelationDao;
import com.kh.pajx.sop.domain.order.ReplaceCardOrder;
import com.kh.pajx.sop.domain.order.ReplaceCardOrderRelation;
import com.kh.pajx.sop.service.order.IReplaceCardOrderRelationService;
import com.kh.pajx.sop.util.common.FormatDate;

/**
 * 补卡单据扩展信息业务逻辑实现类
 * @author:	azzcsimp
 * @Createdate:	2014年12月9日 下午3:28:36
 */
@Service
@Transactional
public class ReplaceCardOrderRelationServiceImpl extends BaseServiceImpl<ReplaceCardOrderRelation> implements
		IReplaceCardOrderRelationService {
	
	@Resource
	private IReplaceCardOrderDao replaceCardOrderDao;
	
	@Resource
	private IReplaceCardOrderRelationDao replaceCardOrderRelationDao;

	@Override
	public void pass(String rcoId, String createOperId, String createOperName) throws Exception {
		// ==> 1.更新扩展信息
		ReplaceCardOrderRelation upRel = replaceCardOrderRelationDao.getMarryRelationByRcoId(rcoId);
		if (null == upRel) {
			throw new Exception("无法获取单据" + rcoId + "扩展信息！");
		}
		upRel.setRelationStatusFlag(0);
		replaceCardOrderRelationDao.update(upRel);

		// ==> 2.新增扩展信息
		ReplaceCardOrderRelation saveRel = new ReplaceCardOrderRelation();
		saveRel.setRcoId(rcoId);
		saveRel.setProgStatus(4);
		saveRel.setPayStatus(upRel.getPayStatus());
		saveRel.setDealTime(FormatDate.dateTimeDetail());
		saveRel.setDealOperId(createOperId);
		saveRel.setDealOperName(createOperName);
		saveRel.setRemark("已通过审核正在制卡中。");
		saveRel.setRelationStatusFlag(1);
		saveRel.setBuyerEmail(upRel.getBuyerEmail());
		saveRel.setTradeNo(upRel.getTradeNo());
		replaceCardOrderRelationDao.save(saveRel);

	}
	
	@Override
	public void turn(String rcoId, String createOperId, String createOperName, String remark) throws Exception {
		// ==> 1.更新扩展信息
		ReplaceCardOrderRelation upRel = replaceCardOrderRelationDao.getMarryRelationByRcoId(rcoId);
		if (null == upRel) {
			throw new Exception("无法获取单据" + rcoId + "扩展信息！");
		}
		upRel.setRelationStatusFlag(0);
		replaceCardOrderRelationDao.update(upRel);

		// ==> 2.新增扩展信息
		ReplaceCardOrderRelation saveRel = new ReplaceCardOrderRelation();
		saveRel.setRcoId(rcoId);
		saveRel.setProgStatus(0);
		saveRel.setPayStatus(upRel.getPayStatus());
		saveRel.setDealTime(FormatDate.dateTimeDetail());
		saveRel.setDealOperId(createOperId);
		saveRel.setDealOperName(createOperName);
		saveRel.setRemark("已被驳回！详细原因：" + remark);
		saveRel.setRelationStatusFlag(1);
		saveRel.setBuyerEmail(upRel.getBuyerEmail());
		saveRel.setTradeNo(upRel.getTradeNo());
		replaceCardOrderRelationDao.save(saveRel);

	}
	
	@Override
	public void close(String rcoId, String createOperId, String createOperName) throws Exception {
		// ==> 1.更新扩展信息
		ReplaceCardOrderRelation upRel = replaceCardOrderRelationDao.getMarryRelationByRcoId(rcoId);
		if (null == upRel) {
			throw new Exception("无法获取单据" + rcoId + "扩展信息！");
		}
		upRel.setRelationStatusFlag(0);
		replaceCardOrderRelationDao.update(upRel);

		// ==> 2.新增扩展信息
		ReplaceCardOrderRelation saveRel = new ReplaceCardOrderRelation();
		saveRel.setRcoId(rcoId);
		saveRel.setProgStatus(9);
		saveRel.setPayStatus(upRel.getPayStatus());
		saveRel.setDealTime(FormatDate.dateTimeDetail());
		saveRel.setDealOperId(createOperId);
		saveRel.setDealOperName(createOperName);
		saveRel.setRemark("已成功完成补卡，单据已关闭！");
		saveRel.setRelationStatusFlag(1);
		saveRel.setBuyerEmail(upRel.getBuyerEmail());
		saveRel.setTradeNo(upRel.getTradeNo());
		replaceCardOrderRelationDao.save(saveRel);
		
	}
	
	@Override
	public void send(String rcoId, String createOperId, String createOperName, String logisticNo, String logisticType) throws Exception {
		ReplaceCardOrder rcod = replaceCardOrderDao.getById(rcoId);
		rcod.setLogisticsNo(logisticNo);
		rcod.setLogisticsType(logisticType);
		replaceCardOrderDao.update(rcod);
		
		
		// ==> 2.更新扩展信息
		ReplaceCardOrderRelation upRel = replaceCardOrderRelationDao.getMarryRelationByRcoId(rcoId);
		if (null == upRel) {
			throw new Exception("无法获取单据" + rcoId + "扩展信息！");
		}
		upRel.setRelationStatusFlag(0);
		replaceCardOrderRelationDao.update(upRel);

		// ==> 3.新增扩展信息
		ReplaceCardOrderRelation saveRel = new ReplaceCardOrderRelation();
		saveRel.setRcoId(rcoId);
		saveRel.setPayStatus(upRel.getPayStatus());
		saveRel.setDealTime(FormatDate.dateTimeDetail());
		saveRel.setDealOperId(createOperId);
		saveRel.setDealOperName(createOperName);
		saveRel.setProgStatus(5);
		saveRel.setRemark("已成功发货！");
		saveRel.setRelationStatusFlag(1);
		saveRel.setBuyerEmail(upRel.getBuyerEmail());
		saveRel.setTradeNo(upRel.getTradeNo());
		replaceCardOrderRelationDao.save(saveRel);

	}

	
	
	
	
	

	@Override
	public List<ReplaceCardOrderRelation> getOrderRelationsByRcoId(String rcoId) throws Exception {
		return replaceCardOrderRelationDao.getOrderRelationsByRcoId(rcoId);
	}

	

	

}
